<?php
/*
    Generic functions used by the rest of the site
    Copyright (C) 2015  Leah Rowe <info@minifree.org>

    This document is released under Creative Commons Attribution-ShareAlike
    4.0 International license.

    See <https://creativecommons.org/licenses/by-sa/4.0/legalcode>
*/

/* Functions */

	/* Minified HTML output (note: needs work for <pre> tags) */
	function miniHtml($strHtml) {
        return $strHtml;
	/*	return $strHtml = preg_replace(
			array("/\r|\n/", "/(\s)+/s", "/\>[^\S ]+/s", "/[^\S ]+\</s"),
			array("", "\\1", ">", "<"),
			$strHtml
		); */
	}

	/* Minified CSS output */
	function miniCss($strCss) {
		return $strCss; // preg_replace("/\r|\n/", "", $strCss);
	}

	/* Generic function for showing a list of URLs */
	function mirrorList($arrayMirrors,$strNotFoundMessage,$forIndexStart,$forIndexbelow) {

		if (count($arrayMirrors)>0) {
			for ($server=$forIndexStart; $server<$forIndexbelow; $server++) {
?>
				<p><a href="<?php echo $arrayMirrors[$server][0]; ?>"><?php echo $arrayMirrors[$server][0]; ?></a> (<?php echo $arrayMirrors[$server][1]; ?>)</p>
<?php
			}
		} else {
?>
				<p><?php echo $strNotFoundMessage; ?></p>
<?php
		}
	}

	/* List rsync mirroring instructions */
	function rsyncList($arrayRsync,$strNotFoundMessage,$forIndexStart,$forIndexBelow) {
		if (
			count($arrayRsync)>0 && ($forIndexStart<=count($arrayRsync)-1)
			&& ($forIndexBelow<=count($arrayRsync)) && ($forIndexStart<=$forIndexBelow)) {
			
			for ($server=$forIndexStart; $server<$forIndexBelow; $server++) {
?>
				<h2>
					<?php echo $arrayRsync[$server][1]; ?>
				</h2>
					<p>
						<b>rsync -avxP --delete --stats <?php echo $arrayRsync[$server][0]; ?> /path/to/docroot/libreboot/</b>
					</p>
<?php
			}
			
		} else {
?>
					<p>
						<?php echo $strNotFoundMessage; ?>
					</p>
<?php
		}
	}

function sanitize_database_input($input) /* for storing in a database */ {
	return htmlspecialchars(addslashes(trim($input)));
}
function sanitize_html_output($input) /* for outputting onto the page */ {
	return htmlspecialchars(trim($input));
}
function decode_input($input){ /* for storing in the PHP variable, when processing it */
	return stripslashes(htmlspecialchars_decode(trim($input)));
}

	// array image example:
	// array(
	// array("path to thumbnail", "path to full image", "caption"),
	//	array("path to thumbnail", "path to full image", "caption"),
	//	array("path to thumbnail", "path to full image", "caption"),
	// )
	// prefix is e.g. "cats", gets added to ID of images
	function gencssboxgallery($arrayImages,$prefix) {
		for($i=0; $i<count($arrayImages); $i++) {
			?>
			<div class="image_item">
			<?php
			$mainpic= ($i==0) ? "product_mainpic ":"";
			?>
				<a title="<?php echo $arrayImages[$i][2]; ?>" id="<?php echo $prefix; ?>image<?php echo (string)($i); ?>" href="#<?php echo $prefix; ?>image<?php echo (string)($i); ?>"><img alt="<?php echo $arrayImages[$i][2]; ?>" class="<?php echo $mainpic; ?>cssbox_thumb" src="<?php echo $arrayImages[$i][0]; ?>" />
					<span class="cssbox_full"><img alt="<?php echo $arrayImages[$i][2]; ?>" src="<?php echo $arrayImages[$i][1]; ?>" /></span>
				</a>
				<a class="cssbox_close" href="#void"></a>
			<?php
			if ($i!=0) {
				?>
				<a class="cssbox_prev" href="#<?php echo $prefix; ?>image<?php echo (string)($i-1); ?>">&lt;</a>
				<?php
			}
			if ($i!=count($arrayImages)-1) {
				?>
				<a class="cssbox_next" href="#<?php echo $prefix; ?>image<?php echo (string)($i+1); ?>">&gt;</a>
				<?php
			}
			?>
			</div>
			<?php
		}
	}


?>
